/**
 * 过滤器
 */
export default {
    install(Vue, config = {}) {

        /**
         * 映射过滤器集合
         */
        Object.entries(Vue.prototype.$filter = {
            /**
             * 数值(限制小数位最大长度)
             * @param {*} value 值
             * @param {*} length 数值小数位长度
             */
            number(value, length = config.number || 2) {
                return new RegExp(`^\\d+\\.\\d{${length + 1},}$`, 'gm').test(value) ? Number(value).toFixed(length) : value;
            },
            /**
             * 货币
             * @param {*} value 值
             * @param {*} symbol 货币符号
             */
            currency(value, symbol = config.currency || '') {
                return (parseFloat(value) || 0).toFixed(2).replace(/^(-)|/, '$1' + symbol);
            },
            /**
             * 日期格式化
             * @param {*} value 值
             * @param {*} exp 表达式
             */
            date(value, exp = config.date || 'date time') {
                function fillZero(number, length) {
                    return new Array(length - String(number).length + 1).join('0') + number;
                }
                if (typeof value == 'string') {
                    value = value.replace(/-/gm, '/');
                }
                if ((value = new Date(value)).valueOf()) {
                    var y = value.getFullYear().toString(),
                        M = value.getMonth() + 1,
                        d = value.getDate(),
                        w = value.getDay(),
                        H = value.getHours(),
                        h = H > 12 ? H - 12 : H,
                        t = H < 12 ? 'AM' : 'PM',
                        m = value.getMinutes(),
                        s = value.getSeconds(),
                        ms = value.getMilliseconds();
                    return typeof exp === 'string' ? exp
                        .replace(/auto/, () => {
                            if (d == new Date().getDate()) {
                                return 'HH:mm';
                            } else if (y == new Date().getFullYear()) {
                                return 'MM/dd';
                            } else {
                                return 'yyyy/MM';
                            }
                        })
                        .replace(/date/, 'yyyy/MM/dd')
                        .replace(/time/, 'HH:mm:ss')
                        .replace(/yyyy/, y)
                        .replace(/yy/, y.substring(2))
                        .replace(/MM/, fillZero(M, 2))
                        .replace(/M/, M)
                        .replace(/dd/, fillZero(d, 2))
                        .replace(/d/, d)
                        .replace(/w/, w)
                        .replace(/W/, ['日', '一', '二', '三', '四', '五', '六'][w])
                        .replace(/HH/, fillZero(H, 2))
                        .replace(/H/, H)
                        .replace(/hh/, fillZero(h, 2))
                        .replace(/h/, h)
                        .replace(/t/, t)
                        .replace(/T/, { AM: '上', PM: '下' }[t])
                        .replace(/zms/, ms)
                        .replace(/ms/, fillZero(ms, 3))
                        .replace(/mm/, fillZero(m, 2))
                        .replace(/m/, m)
                        .replace(/ss/, fillZero(s, 2))
                        .replace(/s/, s)
                        : exp({ y, M, d, H, h, m, s, ms, w })
                }
            },
            /**
             * 数值范围
             * @param {*} value 值
             * @param {*} exp 表达式
             */
            range(value, exp = config.range || '') {
                try {
                    return eval(exp
                        .replace(/\((.*)(,)/, '$1<$2')
                        .replace(/\[(.*)(,)/, '$1<=$2')
                        .replace(/(,)(.*)\)/, '$1<$2')
                        .replace(/(,)(.*)\]/, '$1<=$2')
                        .replace(/,/, `${value}&&${value}`));
                } catch (e) {
                    console.warn('数值范围表达式不合法\n', e);
                }
            }
        }).forEach(([key, value]) => Vue.filter(key, value));

    }
}